package com.tandashi.springbootstudy01.service.strategy.factory;

import com.tandashi.springbootstudy01.service.strategy.service.OrderProcessStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 注意需要使用 postConstruct 实现 strategyMap;
 */
@Component
public class OrderProcessStrategyFactory {

    @Autowired
    private ApplicationContext applicationContext;

    public OrderProcessStrategy getStrategy(String type) {
        return applicationContext.getBean(type + "OrderProcessStrategy",OrderProcessStrategy.class);
    }


    /*public OrderProcessStrategy getStrategy(String type) {
        return orderProcessStrategyMap.get(type);
    }

    @Autowired
    private List<OrderProcessStrategy> OrderProcessStrategys;
    private map<String, OrderProcessStrategy> orderProcessStrategyMap;

    @PostConstruct
    private void postConstruct() {
        orderProcessStrategyMap = OrderProcessStrategys.stream().collect(Collectors.toMap(OrderProcessStrategyFactory::type, Function.identity()));
    }

    private static String type(OrderProcessStrategy strategy) {
        return strategy.getType();
    }*/
}
